/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.cluster;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public final class ClusterEvent {

	private int type;
	private byte[] data;
	/**
	 * Event that notifies node to exit the cluster.
	 * */
	public static final int EXIT = 0;
	public static final int ADD_COMPUTER=-1;
	public static final int REMOVE_COMPUTER=-2;
	
	public ClusterEvent() {
	}

	/**
	 * @param type 	type of the event. Negative integers are reserved for internal use.
	 * @param data data associated with the event
	 * */
	public ClusterEvent(int type, byte[] data) {
		super();
		this.type = type;
		this.data = data;
	}

	/**
	 * Returns type of the event. Negative integers are reserved for internal use.
	 * */
	public int getType() {
		return type;
	}

	public byte[] getData() {
		return data;
	}
	
	public void readFrom(DataInputStream in) throws IOException{
		type = in.readInt();
		final int len = in.readInt();
		data = new byte[len];
		if(len > 0) in.readFully(data);
	}

	public void writeTo(DataOutputStream out) throws IOException {
		out.writeInt(type);
		final int len=data==null? 0: data.length;
		out.writeInt(len);
		if(len > 0) out.write(data);
	}
	
}
